package com.signway.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.DecimalFormat;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Locale;
import java.util.regex.Pattern;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2011-2019, by your Signway, All rights reserved.
 * -----------------------------------------------------------------
 *
 * ProjectName: droid5119
 *
 * Author: Administrator
 *
 * Email: pf.wang@Signway.cn
 *
 * Description: 通用工具类
 *
 * -----------------------------------------------------------------
 * 2019-9-11 : Create CommonTools.java
 * -----------------------------------------------------------------
 */
public class CommonTools {
    private static final String TAG = "CommonTools";

    private static final byte ASCII_0 = 0x30;
    private static final byte ASCII_9 = 0x39;
    private static final byte ASCII_A = 0x41;
    private static final byte ASCII_Z = 0x5A;
    private static final byte ASCII_a = 0x61;
    private static final byte ASCII_z = 0x71;

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean textIsEmpty(String str) {
        return str == null || str.length() == 0 || str.trim().length() == 0;
    }

    /**
     * 判断正则表达式数字0-9
     *
     * @param number
     * @return
     */
    public static boolean isMatchNumberPattern(String number) {
        Pattern numberPattern = Pattern.compile("[0-9]*");
        if (!"".equals(number) && (number != null) && numberPattern.matcher(number).matches()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 数据格式化样式  0.##
     *
     * @param text
     * @return
     */
    public static float FormetFloat(float text) {
        Locale.setDefault(Locale.ENGLISH);
        DecimalFormat fmt = new DecimalFormat("0.##");
        String aa = fmt.format(text);
        return Float.parseFloat(aa);
    }

    /**
     * 获取IPv4地址
     *
     * @return
     */
    public static String getLocalIpv4Address() {
        try {
            if (NetworkInterface.getNetworkInterfaces() != null) {
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                    if (en != null) {

                        NetworkInterface intf = en.nextElement();

                        if (intf == null) {
                            continue;
                        }
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
                                .hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress();
                            }
                        }
                    }
                }
            }
        } catch (SocketException e) {
            LogHelper.d("WifiPreference IpAddress---error-" + e.toString());
        } catch (Exception e) {
            LogHelper.d("WifiPreference IpAddress---error2-" + e.toString());
        }
        return null;
    }

    /**
     * 获取版本名称
     *
     * @param context
     * @return
     * @throws
     */
    public static String getVersionName(Context context) throws Exception {
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名??代表是获取版本信??
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        String version = packInfo.versionName;
        return version;
    }

    /**
     * 比较ASCII数字
     *
     * @param src
     * @param length
     * @return
     */
    public static boolean isAll123(byte[] src, int length) {
        if ((src == null) || (length <= 0) || (src.length < length)) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if ((src[i] <= ASCII_9) && (src[i] >= ASCII_0)) {
                continue;
            }
            return false;
        }
        return true;
    }

    public interface Filter<T> {
        boolean isNeedRemove(T item);
    }

    //过滤指定条件的item
    public static <T> void collectionFilter(Collection<T> list, Filter<T> filter) {
        for (Iterator<T> it = list.iterator(); it.hasNext(); ) {
            T next = it.next();
            if (filter.isNeedRemove(next)) {
                it.remove();
            }
        }
    }

    private static long lastClickTime;

    //双击监听
    public static boolean isDoubleClick() {
        long currentTimeMillis = System.currentTimeMillis();
        boolean isDoubleClick = false;
        if (currentTimeMillis - lastClickTime < 400) {
            isDoubleClick = true;
        }
        lastClickTime = currentTimeMillis;
        return isDoubleClick;
    }

    /**
     * 线程sleep
     * @param msc
     */
    public static void threadSleep(long msc){
        try {
            if(msc > 0){
                Thread.sleep(msc);
            }else{
                Thread.sleep(1);
            }
        } catch (InterruptedException e1) { }
    }
}
